import random
import time
import re
import requests
import json
from lxml import etree
import json
from  selenium import webdriver

import subprocess
from functools import partial
subprocess.Popen = partial(subprocess.Popen, encoding='utf-8')
import execjs
import os
os.environ["NODE_PATH"] = "C:/Users/huo/AppData/Roaming/npm/node_modules"





headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
    "Cookie": "__jdu=1440653569; pinId=FQyhnmeZ3ueSyhkOCZBwig; pin=shanghui1-; unick=shanghui1-; _tp=qLqL%2F%2Fzo1YOoTqY4o6arew%3D%3D; _pst=shanghui1-; shshshfpa=2b6e192c-262a-bc6c-b402-aa49c59cd363-1702947308; shshshfpx=2b6e192c-262a-bc6c-b402-aa49c59cd363-1702947308; TrackID=1gpg3Fzm1YRYLuubsjMq8gcqPBzkKaauheKMm3SKWPryuq8-D74WIsDG627djh3-HcXUbxD5UDMf_GjAJ2lj4i9srAREP_1JSympCZJ00zjE; thor=E5C57F6B4187E501B46EBAC350A8E2AA25D19F8DC736DCD2F4E8F00B60EA064FD4AA718C78E91F2581A35960DD63960E590D147BD33CC77107D8EE6233984371D170FCFE11FE77F95D5698405B1234A17D105110ED61C38071358DAD5DE6AB2E1E691BF0345425203D48622E5CCED3685DA76C3AA3B51ECBA890F9EF1C70D386BC8A005AFE1E2588FB116ABE1455E011; flash=2_tUDrRAtZR9sioXCs5VaDzkJDV-CUjU5S1-QGtbQZKNVnv5CqP3jI-x0B8bmeQR1ORyhYHNql3nhNXYn330cxfIlGPHq0hLMbdoePdNkodbV*; areaId=15; ipLoc-djd=15-1213-3038-59931; Hm_lvt_96bc309cbb9c6a6b838dd38a00162b96=1703558029,1703561252,1703727543,1703813787; jsavif=1; jsavif=1; unpl=JF8EAJlnNSttD0MEDB0CHBASTFwGWw9aH0RWOmNWVFtYHANVHABPRRJ7XlVdXxRLFB9sbxRUWVNKVA4ZBCsSEXteU11bD00VB2xXVgQFDQ8WUUtBSUt-S1tUVl4OQxYCam4GZG1bS2QFGjIbFxVKWlBcWQ9DJwJfYDVkbV1NVQ0SBysTIEptFgoBDEsUBWcqBVFYWUxQBx8FEyIRe14; __jdv=76161171|baidu-search|t_262767352_baidusearch|cpc|171927900482_0_f9a87163360c4ccebdd5b171ffa63ef3|1703833305474; 3AB9D23F7A4B3C9B=NHQOFDWLKHLQAMBULE6UVL33LCFEYESWGXVZVALQCZE2SCLMHKH7O5PAOEJWEIV34JDDXCFO6MHM3N2RCCKX62BA5Q; token=c596ba12f8ae1f1457dca0b0181ceff2,3,946574; __tk=v3k0YckzuSa1XSGxYsb3uSq1v3h5XplTYUuxusawvpT0up2FuSkDYw,3,946574; __jda=181111935.1440653569.1702367905.1703832526.1703832552.33; __jdc=181111935; __jdb=181111935.13.1440653569|33.1703832552; shshshsID=38c1491cee97e001853d25d942b0dce4_13_1703833419596; 3AB9D23F7A4B3CSS=jdd03NHQOFDWLKHLQAMBULE6UVL33LCFEYESWGXVZVALQCZE2SCLMHKH7O5PAOEJWEIV34JDDXCFO6MHM3N2RCCKX62BA5QAAAAMMWRRMKBAAAAAAC472DYOGGEBWYAX; _gia_d=1; Hm_lpvt_96bc309cbb9c6a6b838dd38a00162b96=1703833420; shshshfpb=AAu3GYrSMEm4ZLCYqvGy0AqpJxZzTYxcClHMIfwAAAAA; joyya=1703833320.1703833441.26.0tv6t9e",
    # 'Origin':'https://search.jd.com',
    # 'Referer':'https://search.jd.com/',
    # 'Referer':'https://item.jd.com/10054631882133.html'

    }
try:

    with open('html.txt','r+',encoding='utf-8') as file:
        lines = file.readlines()
        for url in lines:
            print(url)
            # 删除当前行
            file.seek(0)
            lines.remove(url)
            file.truncate(0)
            file.writelines(lines)
            file.seek(0, os.SEEK_END)
            print("https://"+url)
            resp=requests.get("https://"+url,headers=headers)
            text=re.findall(r"colorSize+[^!]+?[]$]",resp.text)[0]
            print(text)
            text=text[11:]
            data=json.loads(text)
            with open ('all.json','a',encoding='utf-8') as f:
                time.sleep(random.randint(2,4))
                f.write("spu:"+url+"\n")
                for i in data:
                    sku=i['skuId']
                    print(sku)
                    newresp=requests.get(f'https://item.jd.com/{sku}.html',headers=headers)
                    html=etree.HTML(newresp.text)
                    image=html.xpath('//*[@id="spec-img"]/@data-origin')[0]
                    i['imageUrl']=image
                    f.write(str(i)+'\n')

except Exception:
    print(Exception)
    # 获取shopId
    # shopId=re.findall("shopId:'[0-9]*",newresp.text)[0][8:]
    #
    # # 获取供应商Id
    # venderId = re.findall("venderId:[0-9]*", newresp.text)[0][9:]
    #
    # body={"skuId":10054631882135,"cat":"670,671,672","area":"15_1213_3038_59931","shopId":"12153530","venderId":12461847,"paramJson":"{\"platform2\":\"1\",\"colType\":0,\"specialAttrStr\":\"p0ppppppppp3ppppppppppppppp\",\"skuMarkStr\":\"00\"}","num":"1","bbTraffic":"","canvasType":1}
    # body['skuId']=sku
    # body['shopId']=shopId
    # body['venderId']=venderId
    #
    #
    # pricePara={
    #     "appid": "pc-item-soa",
    #     "body": 'body',
    #     "client": "pc",
    #     'clientVersion': '1.0',
    #     'functionId': 'pc_detailpage_wareBusiness',
    #     'h5st': '20231226163523114;mmmzgg3miaptg302;fb5df;tk03wd0731d4118nMTzXebtvrH2R9IowFN1DZRD3zmaxWaGuGz3iu3QHcVktNGqXj9lPGfDmphiTah-vyLVrxvGCj3a1;41585c688155d7031a599949131115839aa19c1449c2f6bf0335ff395155a83c;4.2;1703579723114;0aeefaf52c5a7fa31a1ad5e06c8551fda85ad5f4536e7e4aa639c27c742cb035bc404e042da71a6faa85368294121c996264dc0f0def7114979e23a5ff3fcdd6129591dce6a4f0b9d30a6280ece58c866c25c5bb2130947789ff4032bed7103e212c26f15aed9e166f3fc7cd4bef14439e198cf4b500ddf1a2b7d9104b3497e676e0d20c9233fed9a40c72717751db2ff19bf39cd4b563ab058834cf865034b9d1351a1e41bcd2a52e4c3cee33955a77991732142f4c11554dd5ad19f5db014ed3b090395a4f08f78cc6830715f6ff8db3a8da34cfc140b12959e0b5601ccb968d206cd9db84295a5e731e1b4c17ff64d03625cc50d399cce24270e65307e9e59f3794138e8c97e92a305ff8217980a28972a8a8568f56d671fda74021ffe18d6b893032abd7bc9fa50725f9bfa2be2b81318d4dcfaa6bd4f7336d407188277f',
    #     'loginType': '3',
    #     't': '1703579722932',
    #     'uuid': '181111935.1440653569.1702367905.1703570592.1703577683.21',
    #     "x-api-eid-token": 'jdd03NHQOFDWLKHLQAMBULE6UVL33LCFEYESWGXVZVALQCZE2SCLMHKH7O5PAOEJWEIV34JDDXCFO6MHM3N2RCCKX62BA5QAAAAMMUVBY5TQAAAAAC2JNG6ERHYDJPAX',
    #
    # }
    #
    # d = {
    #     "appid": "pc-item-soa",
    #     "functionId": "pc_detailpage_wareBusiness",
    #     "client": "pc",
    #     "clientVersion": "1.0.0",
    #     "t": "1703579722932",
    #     "body": "r"
    # }
    #
    #
    # sha256=open('sha256.js','r',encoding='utf-8')
    # sha256=sha256.read()
    # exc=execjs.compile(sha256)
    #
    # securitys=open('js_security_v3_0.1.8.js','r',encoding='utf-8')
    # securitys=securitys.read()
    # exc1=execjs.compile(securitys)
    #
    #
    #
    # st=exc.call('SHA256',str(body))
    # print(st)
    # pricePara['body']=st
    # d['body']=st
    #
    # appid='item-v3'
    # functionId='recDivinerApi'
    #
    #
    # # with open('d.json', 'w', encoding='utf-8')as f:
    # #     f.write(json.dumps(d))
    #
    # h5st=exc1.call('k',str(body))
    # pricePara['h5st']=h5st
    # print(h5st)
    #
    #
    #
    #
    # priceheader={
    #     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
    #     "Cookie": "__jdu=1440653569; areaId=15; PCSYCityID=CN_330000_330100_0; TrackID=1nY1O1j6KmcrTqgJNdOSUf-Ouj4tjzsVA_raO7SyqDp4p6EedxZ1jiR7Ik_3FkE3YZFvh4OJQDkynY31yFZZVY1mASbszMLti5tWgboovs2w; thor=E5C57F6B4187E501B46EBAC350A8E2AAAE777F5DC860D454896BBFE518DBDD4AFC8B0A2359A792C7CD51641EEB36D05A3C29EDC57C095FCBF74B1DF8A17A8BCBD9C1050E7D7462472528AA77793D911E1196EE8818D9829089E2E5838CB1A01ACC626A0C533A8B2DDF61AE1A2B63721972AF8A13CF7CDD7A712F547CBDD76AE0685AB100FCDC4744F81CDF0E6F930597; flash=2_8pAFQ7YLKSTTw4ZijSakMdcck2eg7Ya5GlGuMycweArsHIRkarPr1a8r3PLcThOgy6zmfuHvHOVl1ylhD0xxh9Aes4_vnu0kx9O_JXKNNws*; pinId=FQyhnmeZ3ueSyhkOCZBwig; pin=shanghui1-; unick=shanghui1-; _tp=qLqL%2F%2Fzo1YOoTqY4o6arew%3D%3D; _pst=shanghui1-; unpl=JF8EALBnNSttXkkEVhlQHBRCSFxXW15bSB4BPGVVBAgKSFIDHQIeEUR7XlVdXxRLFB9vbxRVVFNPUA4aBSsSEXteXVdZDEsWC2tXVgQFDQ8VXURJQlZAFDNVCV9dSRZRZjJWBFtdT1xWSAYYRRMfDlAKDlhCR1FpMjVkXlh7VAQrAh4XEUxZVlpaAHsWM2hXNWRdWUxUAhkyGiIRex8AAlUKTh8HaSoFUVhZTFAHHwUTIhF7Xg; shshshfpa=2b6e192c-262a-bc6c-b402-aa49c59cd363-1702947308; shshshfpx=2b6e192c-262a-bc6c-b402-aa49c59cd363-1702947308; TARGET_UNIT=squnit; jsavif=1; ipLoc-djd=15-1213-3038-59931; __jdv=76161171|baidu-pinzhuan|t_288551095_baidupinzhuan|cpc|0f3d30c8dba7459bb52f2eb5eba8ac7d_0_73ab3c67c20242b283b3aadc2777142e|1702948537985; joyytokem=babel_2TCk8Dpis45pQrNE9VoWy4YgjhbiMDFVT0lxSDk5MQ==.ZHh5Q3Fgf3lIcGJ+cQ8NJgd+RQ0NeDhDNmRjf115eX43QzZkMQgwJD0LPzllGAokRRIZDBAAPhI2eTA5JQUxKzIBFjESCzkHBDgZIg4IMAkUMTc=.9b1b150c; mba_muid=1440653569; token=adb141d872630acad9362d2f70b96e3e,3,946085; __tk=qfzeNAgArAKhqUunNfgDscS3riu4qUzgrDS3scq5qLqBOcTfNirhsG,3,946085; __jda=143920055.1440653569.1702367905.1702634643.1702947306.9; __jdc=143920055; 3AB9D23F7A4B3C9B=NHQOFDWLKHLQAMBULE6UVL33LCFEYESWGXVZVALQCZE2SCLMHKH7O5PAOEJWEIV34JDDXCFO6MHM3N2RCCKX62BA5Q; joyya=1702950147.1702954804.26.1dy6vgv; __jdb=143920055.27.1440653569|9.1702947306; shshshsID=fd9d81bec53e3bb352c3084732099a2e_14_1702954843392; 3AB9D23F7A4B3CSS=jdd03NHQOFDWLKHLQAMBULE6UVL33LCFEYESWGXVZVALQCZE2SCLMHKH7O5PAOEJWEIV34JDDXCFO6MHM3N2RCCKX62BA5QAAAAMMQACL3XQAAAAAC5KPYIEEEJUUX4X; _gia_d=1; shshshfpb=AApPABICMEm4ZLCYqvGy0AqpJxZzTYxcClHMITQAAAAA",
    #     'Origin':'https://item.jd.com',
    #     'Referer':'https://item.jd.com/'
    # }
    # price=requests.get('https://api.m.jd.com',headers=priceheader,params=pricePara)
    # print(price.text)
    # print(i)


